package com.baox.agridata.synchronous.assembly;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baox.agridata.synchronous.constant.Constant;
import com.baox.agridata.synchronous.entity.quanguo.Jyzscpb;
import com.baox.agridata.synchronous.entity.shanxi.TCCOutPAEntity;
import com.baox.agridata.synchronous.service.shanxi.TBASEDesignatedPigSlaughteringCompanyService;
import com.baox.agridata.synchronous.service.shanxi.TBaseRegionService;
import com.baox.agridata.synchronous.util.DateUtil;
import com.baox.agridata.synchronous.util.DicDataTurnUtil;
import com.baox.agridata.synchronous.util.MapUtil;
import com.baox.agridata.synchronous.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 全国到达本省的产品A数据
 */
@Component
public class TCCOutPAAssembly {


    @Autowired
    private TBaseRegionService tBaseRegionService;

    @Autowired
    private TBASEDesignatedPigSlaughteringCompanyService tbaseDesignatedPigSlaughteringCompanyService;

    public TCCOutPAEntity JyzqgcpaToOutProductA(Jyzscpb oldEntity) {
        TCCOutPAEntity newEntity = null;
        if (oldEntity != null) {
            newEntity = new TCCOutPAEntity();


            String UUID = oldEntity.getId();
            newEntity.setMid(UUID.replace("-","")); //mid
            newEntity.setSource_id(null);                        //source_id
            newEntity.setName(null);                             //name
            newEntity.setUUID(UUID);         //UUID
            newEntity.setCCType(Constant.PRODUCT_A);             //CCType
            newEntity.setQRCode(oldEntity.getEwm());             //QRCode
            newEntity.setFactoryCode(oldEntity.getZbh());        //FactoryCode
            newEntity.setOwner(oldEntity.getHz());               //Owner
            newEntity.setOwnerTel(oldEntity.getHzdh());          //OwnerTel
            newEntity.setAmount(oldEntity.getSl());              //Amount
            newEntity.setAmountUnified(oldEntity.getSl());        //AmountUnified
            newEntity.set_PartId(Constant._PartId);               //_PartId

            String l_RegionCode = oldEntity.getQqhdm() + "000000";
            Map startRegion = tBaseRegionService.selectRegionInfoByRegionCodeForAnimalAndProduct(l_RegionCode);
            if (startRegion != null) {
                String l_RegionMap = JSON.toJSONString(startRegion);
                newEntity.setStartingPlaceRegion(l_RegionMap);       //StartingPlaceRegion
            }
            newEntity.setStartingPlaceName(oldEntity.getQdmc());      //StartingPlaceName

            String l_desRegionCode = oldEntity.getDqhdm() + "000000";
            Map desRegion = tBaseRegionService.selectRegionInfoByRegionCodeForAnimalAndProduct(l_desRegionCode);
            if (desRegion != null) {
                String l_RegionMap = JSON.toJSONString(desRegion);
                newEntity.setDestinationPlaceRegion(l_RegionMap);                  //DestinationPlaceRegion
                newEntity.setDestinationPlaceRegionID(MapUtil.getInteger(desRegion,"ID"));          //DestinationPlaceRegionID
                newEntity.setDestinationPlaceRegionRI1(MapUtil.getInteger(desRegion,"RI1"));        //DestinationPlaceRegionRI1
                newEntity.setDestinationPlaceRegionRI2(MapUtil.getInteger(desRegion,"RI2"));        //DestinationPlaceRegionRI2
                newEntity.setDestinationPlaceRegionRI3(MapUtil.getInteger(desRegion,"RI3"));        //DestinationPlaceRegionRI3
                newEntity.setDestinationPlaceRegionRI4(MapUtil.getInteger(desRegion,"RI4"));        //DestinationPlaceRegionRI4
                newEntity.setDestinationPlaceRegionRI5(MapUtil.getInteger(desRegion,"RI5"));        //DestinationPlaceRegionRI
            }
            newEntity.setDestinationPlaceName(oldEntity.getDdmc());                  //DestinationPlaceName
            newEntity.setCarrier(oldEntity.getYsmc());                               //Carrier
            newEntity.setCarrierTel(oldEntity.getYsdh());                            //CarrierTel
            newEntity.setTransportNumber(oldEntity.getYsph());                       //TransportNumber
            newEntity.setDisinfectType(oldEntity.getYsxd());                         //DisinfectType
            newEntity.setAgengcyName(oldEntity.getQfdw());                           //AgengcyName
            newEntity.setQuarantineOfficerName(oldEntity.getGfsyxm());               //QuarantineOfficerName
            newEntity.setDateOfIssue(DateUtil.dateFormat.format(oldEntity.getRq()));   //DateOfIssue
            newEntity.setRemark(oldEntity.getBz());                                  //Remark
            newEntity.setAgencyTel(oldEntity.getQfdh());                             //AgencyTel
            newEntity.setCreator_id(Constant.CREATOR_ID);                            //creator_id
            newEntity.setModifier_id(Constant.CREATOR_ID);                           //modifier_id
            newEntity.setPartition_id(Constant.PARTITION_ID);                        //partition_id
            newEntity.setPartition_ids(Constant.PARTITION_IDS);                      //partition_ids
            newEntity.setCategory_id("4503e8d1a2c34cc380baeea10ef801a3");            //category_id
            newEntity.setDisabled("f");                                              //disabled
            newEntity.setDeleted("f");                                               //deleted
            newEntity.setApp_id("4dcb17fbbcc44f19b724efd9d7275302");                 //app_id

            JSONObject _CheckResult = new JSONObject();
            _CheckResult.put("key", 2803);
            _CheckResult.put("Name", "未检查");
            newEntity.setCheckResult(_CheckResult.toJSONString());                    //CheckResult

            JSONObject _CCOutInType = new JSONObject();
            _CCOutInType.put("key", 1901);
            _CCOutInType.put("Name", "入省");
            newEntity.setCCOutInType(_CCOutInType.toJSONString());                    //CCOutInType

            if (StringUtil.isNotEmpty(oldEntity.getDwdm())) {
                Map animalTypeMap = DicDataTurnUtil.animalTypeTurnMap(oldEntity.getDwdm());
                String animalType = JSON.toJSONString(animalTypeMap);
                newEntity.setAnimalType(animalType);                      //AnimalType
            }

            if (StringUtil.isNotEmpty(oldEntity.getCplxdm())) {
                Map productTypeMap = DicDataTurnUtil.productTypeTurnMap(oldEntity.getCplxdm());
                String productType = JSON.toJSONString(productTypeMap);
                newEntity.setProductType(productType);                  //ProductType
            }

            if (oldEntity.getSldw() != null) {
                newEntity.setUnit(JSON.toJSONString(DicDataTurnUtil.UnitTurnMap(oldEntity.getSldw())));   //Unit
            }

            if (oldEntity.getQlbdm() != null && !oldEntity.getQlbdm().equals("")) {
                newEntity.setStartingPlaceType(JSON.toJSONString(DicDataTurnUtil.PlaceTypeTurnMap(oldEntity.getQlbdm())));    //StartingPlaceType
            }


            JSONObject StartingCompany = new JSONObject();
            StartingCompany.put("UUID", StringUtil.isEmpty(oldEntity.getQqydm()) ? "" : oldEntity.getQqydm());
            StartingCompany.put("Name", oldEntity.getCpscmc());
            if (StringUtil.isNotEmpty(oldEntity.getQqydm()) && !"null".equals(oldEntity.getQqydm())) {  //起运地代码不为空的情况下
                Map xdrMap = tbaseDesignatedPigSlaughteringCompanyService.getBASEDesignatedPigSlaughteringCompany(oldEntity.getCpscid());  //产品从全国屠宰场出发
                if (xdrMap != null) {
                    StartingCompany.put("UUID", xdrMap.get("mid").toString());
                    StartingCompany.put("Name", xdrMap.get("name").toString());
                }
            }
            newEntity.setStartingPlaceCompany(StartingCompany.toJSONString());           //StartingPlaceCompany

            JSONObject desCompany = new JSONObject();
            desCompany.put("UUID", StringUtil.isEmpty(oldEntity.getDqydm()) ? "" : oldEntity.getDqydm());
            desCompany.put("Name", oldEntity.getDdmc());
            newEntity.setDestinationPlaceCompany(desCompany.toJSONString());     //DestinationPlaceCompany



            if(StringUtil.isNotEmpty(oldEntity.getDlbdm())) {
                newEntity.setDestinationPlaceType(JSON.toJSONString(DicDataTurnUtil.PlaceTypeTurnMap(oldEntity.getDlbdm())));   //DestinationPlaceType
            }

            if(oldEntity.getYsfs() != null && !oldEntity.getYsfs().equals("")) {
                newEntity.setTransportType(JSON.toJSONString(DicDataTurnUtil.TransportTypeTurnMap(oldEntity.getYsfs())));        //TransportType
            }

            if(oldEntity.getZddyxq() != null && !oldEntity.getZddyxq().equals("")){
                newEntity.setValidityDays(JSON.toJSONString(DicDataTurnUtil.ValidityDaysTurnMap(oldEntity.getZddyxq())));           //ValidityDays
            }

            JSONObject _Status = new JSONObject();
            _Status.put("key", Constant.RECEIVED_UN_RECEIVE);
            _Status.put("Name","未回收");
            newEntity.setStatus(_Status.toJSONString());             //Status

            JSONObject _PrintStatus = new JSONObject();
            _PrintStatus.put("key", 1801);
            _PrintStatus.put("Name","已打印");
            newEntity.setPrintStatus(_PrintStatus.toJSONString());    //PrintStatus


            JSONObject _IsOnlineIssuing = new JSONObject();
            _IsOnlineIssuing.put("key", 2301);
            _IsOnlineIssuing.put("Name","在线");
            newEntity.setIsOnlineIssuing(_IsOnlineIssuing.toJSONString());        //IsOnlineIssuing
        }
        return newEntity;
    }

}
